[#ftl]
[@define "agile._EditorWidget","_WidgetBase,_TemplatedMixin,_WidgetsInTemplateMixin"]
	//widgetsInTemplate:true,
	context:null
	//,templateString:"<table><tr><td data-dojo-attach-point='labelNode' style='width:120px;cursor:help'></td><td><div data-dojo-attach-point='editor' data-dojo-type='{this_editorClass}' placeHolder='{this_placeHolder}'></div></td></tr></table>"
	,constructor:function(){
		this.declaredClass = this.constructor.prototype.declaredClass;
		
		this.editorMetadata = arguments[0].editorMetadata;		
		this.context = arguments[0].context;	

		this.name = arguments[0].editorMetadata.name;
				
		if(this.editorMetadata.editorClass.indexOf("dijit") == 0){
			agile.requireX(this.editorMetadata.editorClass);	
		}
		aspect.after(this,"startup",lang.hitch(this,function(){
			if(!this._startedX){
				this.onStartup();
				this._startedX = true;
			}
		}));
	}
	,postMixInProperties:function(){
		this.templateString = this.editorMetadata.getTemplateString();	
		this.templateString = lang.replace(this.templateString,agile.i18nValues(this.templateString,this.editorMetadata));		
	}	
	
	,onStartup:function(){
		this.editorMetadata.onStartup(this);
				
		var listener= lang.hitch(this,function(name,oldValue,newValue){
//			console.info("######value change:",name,oldValue,newValue,"_fireOnChange=",this._fireOnChange);
			if(this.context != null && this._fireOnChange){
				if(this.editor.isValid == undefined || (typeof this.editor.isValid == "function" && this.editor.isValid())){
					//this.editorMetadata.onValueChange(this.context.getCurrentWidget(),this.editorMetadata.name,this.editorMetadata.parse(newValue));
					var target = this.context.getCurrentWidget();
					name = this.editorMetadata.name;
					var beforeValue = target.metadata("parameters." + name);
					var afterValue = this.editorMetadata.parse(newValue);
					this.editorMetadata.onValueChange(target,name,afterValue);
					agile.commit([{action:"update",target:target,category:"parameters",property:name,beforeValue:beforeValue,afterValue:afterValue}]);	
				}				
			}
//			this._fireOnChange = true;
		});
				
		if(this.editorMetadata.editorClass == "dijit/form/CheckBox"){
			this.editor.watch("checked",listener);
		}else{			
			this.editor.watch("value",listener);		
//			this.own(on(this.editor,"keyDown",function(event){
//	//			console.info("@@@@@@@@@@editor keyDown");
//				event.stopPropagation();
//			}));
//			this.own(on(this.editor,"keyUp",function(event){
//	//			console.info("@@@@@@@@@@editor keyup");
//				event.stopPropagation();
//			}));
		}
		this.initialize(this.editorMetadata,this.context);
	}
	
	,get:function(name){
		if(name == "value"){
			return this.editor.get("value");
		}
		return this.inherited(arguments);
	}
	,set:function(name,value){
//		console.log("set=",this.name,name,value);
		if(name == "value"){
			this.editor.set("value",value);
		}else{
			this.inherited(arguments);
		}
	}
	,reset1:function(name,value){
//		if(this.editor.declaredClass.indexOf("CheckBox") != -1){
//			console.log("reset=",this.name,name,value,this.editor.declaredClass);
//		}
		if(name == "value"){
			this._fireOnChange = false;	
			if(this.editor.declaredClass.indexOf("CheckBox") != -1){
				this.editor.set("checked",value);
			}else{
				this.editor.set("value",value);
			}
			this._fireOnChange = true;
		}else{
			this.set(name,value);
		}
	}
	
	,isValid:function(){
		return this.editor.isValid();
	}
	,release: function(){
		//console.log("release*******",this.editorMetadata);
		this.context = null;
		this.editorMetadata = null;
	}
	,initialize: function(editorMetadata,context){
//		//console.log("initialize*******",editorMetadata.name);
		this.editorMetadata = editorMetadata;		
		this.context = context;
//		console.error(this.declaredClass,"initialize _EditorWidget.context",this.context);
		if(this.labelNode && editorMetadata){
			this.labelNode.innerHTML = editorMetadata.label;
			this.labelNode.title = editorMetadata.tooltip;
		}
		
		if(this.editor && editorMetadata){
			this.editor.name = editorMetadata.name;	
			var ownerWidget = this.context.getCurrentWidget();
			if(ownerWidget){
				this.editor.set("readOnly",editorMetadata.readOnly(ownerWidget));
				this.editor.set("disabled",editorMetadata.disabled(ownerWidget));
			}
			this.editor.set("placeHolder",editorMetadata.placeHolder);
			if(agile.isIE && agile.isIE < 11){				
				this.editor.set("disabled",this.editor.get("readOnly"));				
			}
//			this.editor.set("title",editorMetadata.tooltip);
//			this.editor.set("promptMessage",editorMetadata.tooltip);			
		}
		if(editorMetadata){
			editorMetadata.initializeEditor(this);
		}
	}
	,getEditors:function(){
		var editors = {};
		if(this.editorMetadata){			
			editors[this.editorMetadata.name] = this;
		}
		return editors;
	}
[/@]

[@define "agile._XEditorWidget","agile._EditorWidget"]
	templateString:""
	,constructor:function(){
		this.editors = {};
//		this.propertyEditors = [];
	}
	,onStartup:function(){
//		console.warn(this.declaredClass,"_XEditorWidget.context",this.context);
		query(".xeditor",this.domNode).forEach(function(node,index){
			var editor = registry.byNode(node);
			this.editors[editor.name] = editor;
			editor.set("context",this.context);
			agile.stopKeyEvent(editor);
		},this);
		
		if(this.editorMetadata){
//			this.editorMetadata.onStartup(this);
//			this.editorMetadata.initializeEditor(this);
			this.initialize(this.editorMetadata,this.context);
		}		
	}
	,initialize: function(editorMetadata,context){
		this.editorMetadata = editorMetadata;		
		this.context = context;
		for(var name in this.editors){
			this.editors[name].initState(context);
		}
	}	
	
	,get:function(name){
		if(name == "value"){
			var value = {}; var hasValue = false;
			for(var name in this.editors){
				var v = this.editors[name].get("value");
				if(v != null){
					value[name] = v;
					hasValue = true;
				}				
			}
//			//console.log(this.declaredClass,"[agile._XEditorWidget] get value:",value);
			return hasValue?value:null;
		}
		return this.inherited(arguments);
	}
	,set:function(name,value){
		if(name == "value"){
			for(var name in this.editors){
				this.editors[name].reset1(name,value);
			}
		}else{
			this.inherited(arguments);
		}
	}
	,reset1:function(name,value){
//		console.log("2##########reset1",name,value)
		if(value){
			for(var name in this.editors){
				this.editors[name].reset1("value",value);
			}
		}else{
			for(var name in this.editors){
				this.editors[name].reset1("value","");
			}
		}
	}
	,isValid:function(){
//		//console.log(this.declaredClass,"[agile._XEditorWidget] isValid...");
		for(var name in this.editors){
			if(this.editors[name] && !this.editors[name].isValid()){
				return false;
			}
		}
		return true;
	}
[/@]